#!/bin/bash
# $Id: sspm 728 2009-09-27 00:17:20Z slakmagik $
# Written by slakmagik <jsun freeshell org> (usual symbols in spaces)
# Released under the WTFPL

script=${0##*/}

# USER-MODIFIABLE VARIABLES
# this needs to be where the checked out svn directory is.
repo="/tmp/sbopkg-read-only"
remote_repo="http://sbopkg.googlecode.com/svn/trunk/"
export OUTPUT="${OUTPUT:-/tmp}"

usage() {
cat << EOF
$script: sbopkg subversion package maker usage: $script [-b] | [-i] | [-s]

-b build a package from local subversion repository
-i install the package
-s syncs the local repo to the remote one

No arguments is equivalent to -bis: $script checks out the latest revision to
the subversion repository and builds and installs a package from it.
EOF
exit
}

if (( $# == 0 )); then
    build="yes"
    inst="yes"
    sync="yes"
else
    build="no"
    inst="no"
    sync="no"
fi

while getopts :bis opt; do
    case $opt in
        b) build="yes" ;;
        i) inst="yes" ;;
        s) sync="yes" ;;
        *) usage ;;
    esac
done

# Make sure we are root.
if (( $(id -u) != 0 )); then
    echo "$script: $script must be run by the root user." 1>&2
    exit 1
fi

if [[ $sync == yes ]]; then
    if [[ -w $repo ]] || [[ ! -e $repo && -w ${repo%/*} ]]; then
        svn checkout $remote_repo $repo ||
            { echo "$script: checkout failed" 1>&2; exit 1; }
    else
        printf "$script: $repo is not writable.\n" 1>&2
        printf "Change your privileges or the value of the \"repo\" " 1>&2
        printf "variable.\n" 1>&2
        exit 1
    fi
fi

# this allows for a version mismatch if someone builds a package, doesn't
# install it, then syncs their repo to a newer version, then does install the
# old package. But, as the doctor said to the guy who complained that it hurt
# when he did that, don't DO that.
if [[ $build == yes || $inst == yes ]]; then
    svn_script="$repo/src/usr/sbin/sbopkg"
    if [ -r $svn_script ]; then
        export VERSION=svn$(svnversion $repo)
    else
        echo "$script: unable to find $svn_script." 1>&2
        echo "Please check your repository." 1>&2
        exit 1
    fi

    if [[ $OUTPUT =~ "$repo/(sbopkg-$VERSION|root-tools)" ]]; then
        printf "$script: the OUTPUT variable is set to a path that will " 1>&2
        printf "be removed.\n" 1>&2
        printf "Please alter the value of the variable.\n" 1>&2
        exit 1
    fi

    output_pkg=$OUTPUT/sbopkg-$VERSION-noarch-1_cng.tgz
    if [[ $build == yes ]]; then
        cd $repo
        if [ -d sbopkg-$VERSION ] || [ -d root-tools ]; then
            echo "$script: director(y|ies) exist" 1>&2; exit 1
        fi
        cp -r src sbopkg-$VERSION
        cp -r tools root-tools
        tar --exclude='*.svn*' --exclude='*.rej' \
            --exclude='*~' --exclude='tags' \
            -czvf $repo/root-tools/sbopkg-$VERSION.tar.gz sbopkg-$VERSION
        cd $repo/root-tools
        sh sbopkg.SlackBuild
        cd $repo
        rm -rf root-tools
        rm -rf sbopkg-$VERSION
    fi
    if [[ $inst == yes ]]; then
        if [ -r $output_pkg ]; then
            upgradepkg --install-new --reinstall $output_pkg
        else
            echo "$script: $output_pkg not found." 1>&2; exit 1
        fi
    fi
fi
